#!/user/bin/python
# -*- coding: utf-8 -*-
'''
 @Time    : 2018/5/2 23:02
 @File    : client_namespace.py
 @desc    :
'''

from socketIO_client import BaseNamespace

from commonSpiders.creeper.utils.namespace_utils import get_extend_context
from commonSpiders.net.message.socketio_msg import Failure


def service_excute(namespace, service_key, action_key, data):
    '''
    调用服务
    :param namespace:
    :param service_key:
    :param action_key:
    :param data:
    :return:
    '''
    context = get_extend_context(namespace)
    service = context.get(service_key, None)
    if service:
        action = getattr(service, action_key, None)
        if action:
            msg = action(data)
        else:
            msg = Failure(error='没有扩展服务')
    else:
        msg = Failure(error='没有扩展服务')
    return msg


class ClientBaseNamespace(BaseNamespace):
    KEY = '/base'

    REGISTER = False

